--编写者：alt
--功能介绍：修复原版容器和物品栏共存一个实体时的bug
--使用方法：直接粘贴到模组环境 需要修改herta_dolls为对应的实体tag

--修复bug
local InventoryItem = require "components/inventoryitem"
local oldRemoveFromOwner = InventoryItem.RemoveFromOwner
function InventoryItem:RemoveFromOwner(wholestack, keepoverstacked)
    if self.owner and self.owner:HasTag("herta_dolls") then
        return self.owner.components.container and self.owner.components.container:GetItemSlot(self.inst) and
            self.owner.components.container:RemoveItem(self.inst, wholestack, nil, keepoverstacked)
            or self.owner.components.inventory and
            self.owner.components.inventory:RemoveItem(self.inst, wholestack, nil, keepoverstacked)
    end
    return oldRemoveFromOwner(self, wholestack, keepoverstacked)
end

local oldOnRemoveEntity = InventoryItem.OnRemoveEntity
function InventoryItem:OnRemoveEntity(...)
    if self.owner and self.owner:HasTag("herta_dolls") then
        if self.owner then
            if self.owner.components.container and self.owner.components.container:GetItemSlot(self.inst) then
                self.owner.components.container:RemoveItem(self.inst, true)
            else
                return oldOnRemoveEntity(self, ...)
            end
        end
        TheWorld:PushEvent("forgetinventoryitem", self.inst)
        return
    end

    return oldOnRemoveEntity(self, ...)
end